//
//  SecondScene.cpp
//  SceneTrans1
//
//  Created by Jaewhan Lee on 12. 12. 8..
//
//

#include "SecondScene.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"




using namespace cocos2d;
using namespace CocosDenshion;

CCScene* SecondScene::scene()
{
    CCScene *scene = CCScene::create();
    
    SecondScene *layer = SecondScene::create();
    
    scene->addChild(layer);
    
    return scene;
}

bool SecondScene::init()
{
    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )
    {
        return false;
    }
    
	
	// speed
	speed=3.0;
    /////////////////////////////
    
	this->schedule(schedule_selector(SecondScene::collisionCheck));
	collision=0;

    move1 = CCMoveTo::create(3.0f, ccp(380,430));
	move2 = CCMoveTo::create(3.0f, ccp(380,430));
    // 메뉴 아이템 생성 및 초기화
    
	
    
    
	// 게임 뒷 배경
	background = CCSprite::create("gameBack.png");
	background->setPosition(ccp(160, 240));
    //body->setScale(0.5);
	this->addChild(background);

	// making chain
	chain1 = CCSprite::create("chain1.png");
    chain1->setPosition(ccp(220, 60));
//    chain1->setScale(0.5);
    this->addChild(chain1);
	chain2 = CCSprite::create("chain2.png");
    chain2->setPosition(ccp(220, 60));
//    chain2->setScale(0.5);
    this->addChild(chain2);
	chain3 = CCSprite::create("chain3.png");
    chain3->setPosition(ccp(220, 60));
//    chain3->setScale(0.5);
    this->addChild(chain3);
	chain4 = CCSprite::create("chain4.png");
    chain4->setPosition(ccp(220, 60));
//    chain4->setScale(0.5);
    this->addChild(chain4);
	chain5 = CCSprite::create("chain2.png");
    chain5->setPosition(ccp(220, 60));
//    chain4->setScale(0.5);
    this->addChild(chain5);
	chain6 = CCSprite::create("chain1.png");
    chain6->setPosition(ccp(220, 60));
//    chain4->setScale(0.5);
    this->addChild(chain6);
	chain7 = CCSprite::create("chain3.png");
    chain7->setPosition(ccp(220, 60));
//    chain4->setScale(0.5);
    this->addChild(chain7);
	
	
	// making hand
    hand = CCSprite::create("hand.png");
    hand->setPosition(ccp(220, 90));
    hand->setScale(0.5);
    this->addChild(hand);
	
	// making body
	body = CCSprite::create("body.png");
    body->setPosition(ccp(160, 30));
    body->setScale(0.5);
    this->addChild(body);
	
	// making vayne
	vayne = CCSprite::create("vayne.png");
    vayne->setPosition(ccp(-150, 430));
    vayne->setScale(0.7);
    this->addChild(vayne);
	// making vayne2
	vayne2 = CCSprite::create("vayne.png");
    vayne2->setPosition(ccp(-200, 430));
    vayne2->setScale(0.7);
    this->addChild(vayne2);

    CCLog("init");

	// 버튼(블리츠 몸통임ㅋ)
	item1 = CCMenuItemFont::create("     \n     \n     \n     ",
                                                   this,
                                                   menu_selector(SecondScene::doClose)
                                                   );
    item1->setColor(ccc3(0, 0, 0));
	item1->setFontSize(10);
	
	

    // 메뉴 생성
    CCMenu* pMenu = CCMenu::create( item1, NULL );
    
    // 메뉴 위치
    pMenu->setPosition(140, 40);

//	pMenu->setScale(5.0);
    
    // 레이어에 메뉴 객체 추가
    this->addChild(pMenu);


	// 점수
	score=0;
	scoreLabel= CCLabelTTF::create("0","Thonburi",50);
	scoreLabel->setPosition(ccp(240,160));
	this->addChild(scoreLabel);


    return true;
}

void SecondScene::collisionCheck(float temp){
	if(abs(vayne->getPositionX() - hand->getPositionX()) < 20 && hand->getPositionY() > 470){
		//CCLog("fuck");
		collision=1;
		vayne->stopAction(move1);
		
	}
	if(collision==1){

		

		// 점수 증가
		char scoreTemp[20];
		
		//scoreTemp=score-48;
		
		//scoreLabel = CCMenuItemFont::create(&scoreTemp);
		//score->setColor(ccc3(0, 0, 0));
		//scoreLabel->setScale(2.0);
		//score->setFontSize(300);
		//scoreLabel->setPosition(-100,0);
    
		itoa(score,scoreTemp,10);

		
		scoreLabel->setString(scoreTemp);
		
		

		// 베인 딸려옴
		vayne->setPositionX(hand->getPositionX());
		vayne->setPositionY(hand->getPositionY());
		if(vayne->getPositionY()==90.0){
			score++;
			speed=speed-0.4;
			vayne->setPositionX(-150.0);
			vayne->setPositionY(430.0);
			collision=0;
			move1 = CCMoveTo::create(speed, ccp(380,430));
			vayne->runAction(move1);
		}
	}
	
	else if(vayne->getPositionX()==380.0){
			vayne->setPositionX(-150.0);
			vayne->setPositionY(430.0);
			 move1 = CCMoveTo::create(speed, ccp(380,430));
			vayne->runAction(move1);
			
	}
	


	//CCLog("%f %f",vayne->getPositionX(),hand->getPositionX());
}

void SecondScene::onEnter()
{
    CCLayer::onEnter();

    CCLog("onEnter");
}

void SecondScene::onEnterTransitionDidFinish()
{

	
	
	
	vayne->runAction(move1);
	//vayne2->runAction(move2);
	

    CCLayer::onEnterTransitionDidFinish();

    CCLog("onEnterTransitionDidFinish");
}

void SecondScene::onExitTransitionDidStart()
{
    CCLayer::onExitTransitionDidStart();
    
    CCLog("onExitTransitionDidStart");
}

void SecondScene::onExit()
{
    CCLayer::onExit();
    
    CCLog("onExit");
}

SecondScene::~SecondScene()
{
    CCLog("dealloc");
}

void SecondScene::doClose(CCObject* pSender)
{
	

	// chain
	CCActionInterval* chMoving1 = CCMoveTo::create(0.4f, ccp(170,430));
	CCActionInterval* chMoving11 = CCMoveTo::create(0.4f, ccp(220, 60));
	CCActionInterval* chMoving2 = CCMoveTo::create(0.4f, ccp(180,390));
	CCActionInterval* chMoving22 = CCMoveTo::create(0.4f, ccp(220, 60));
	CCActionInterval* chMoving3 = CCMoveTo::create(0.4f, ccp(185,323));
	CCActionInterval* chMoving33 = CCMoveTo::create(0.4f, ccp(220, 60));
	CCActionInterval* chMoving4 = CCMoveTo::create(0.4f, ccp(193,260));
	CCActionInterval* chMoving44 = CCMoveTo::create(0.4f, ccp(220, 60));
	CCActionInterval* chMoving5 = CCMoveTo::create(0.4f, ccp(205,195));
	CCActionInterval* chMoving55 = CCMoveTo::create(0.4f, ccp(220, 60));
	CCActionInterval* chMoving6 = CCMoveTo::create(0.4f, ccp(217,145));
	CCActionInterval* chMoving66 = CCMoveTo::create(0.4f, ccp(220, 60));
	CCActionInterval* chMoving7 = CCMoveTo::create(0.4f, ccp(222,90));
	CCActionInterval* chMoving77 = CCMoveTo::create(0.4f, ccp(220, 60));

//	CCFiniteTimeAction* chainSeq=CCSpawn::create(chMoving1,chMoving2,chMoving3,chMoving4,chMoving5,chMoving6,chMoving7, NULL);

	CCFiniteTimeAction* chainSeq1=CCSequence::create(chMoving1,chMoving11, NULL);
	CCFiniteTimeAction* chainSeq2=CCSequence::create(chMoving2,chMoving22, NULL);
	CCFiniteTimeAction* chainSeq3=CCSequence::create(chMoving3,chMoving33, NULL);
	CCFiniteTimeAction* chainSeq4=CCSequence::create(chMoving4,chMoving44, NULL);
	CCFiniteTimeAction* chainSeq5=CCSequence::create(chMoving5,chMoving55, NULL);
	CCFiniteTimeAction* chainSeq6=CCSequence::create(chMoving6,chMoving66, NULL);
	CCFiniteTimeAction* chainSeq7=CCSequence::create(chMoving7,chMoving77, NULL);


	chain1->runAction(chainSeq1);
	chain2->runAction(chainSeq2);
	chain3->runAction(chainSeq3);
	chain4->runAction(chainSeq4);
	chain5->runAction(chainSeq5);
	chain6->runAction(chainSeq6);
	chain7->runAction(chainSeq7);
	
	// hand
	CCActionInterval* move1 = CCMoveTo::create(0.4f, ccp(160,480));
	CCActionInterval* move2 = CCMoveTo::create(0.4f, ccp(220,90));

	CCFiniteTimeAction* seq=CCSequence::create(move1,move2, NULL);

	hand->runAction(seq);


	// button
	CCActionInterval* move3 = CCMoveTo::create(0.1f, ccp(0,-480));
	CCActionInterval* move4 = CCMoveTo::create(1.4f, ccp(0,0));
	CCFiniteTimeAction* seq2=CCSequence::create(move3,move4, NULL);

	item1->runAction(seq2);

	SimpleAudioEngine::sharedEngine()->playEffect("shoot.wav");


////    CCDirector::sharedDirector()->popScene();
//
//    CCScene* pScene = HelloWorld::scene();
//    CCDirector::sharedDirector()->replaceScene( pScene );
////    CCDirector::sharedDirector()->replaceScene( CCTransitionProgressRadialCCW::create(1, pScene) );
//
}







